package com.ajie.service.impl;


import com.ajie.entity.SysMenu;
import com.ajie.mapper.SysMenuMapper;
import com.ajie.service.SysMenuService;
import com.ajie.utils.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {


    @Autowired
    private SysMenuMapper MenuMapper;
    
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Result findPage(@RequestBody QueryInfo queryInfo) {
        log.info("该文件名是：SysMenuServiceImpl,该方法是：findPage");
        log.info("1.开始菜单数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
        PageHelper.startPage(queryInfo.getPageNumber(),queryInfo.getPageSize());
        Page<SysMenu> page = MenuMapper.findPage(queryInfo.getQueryString());
        long total = page.getTotal();
        List<SysMenu> result = page.getResult();
        log.info("查询的总条数是-->{}",total);
        log.info("分页列表-->{}",result);
        return new PageResult(total, result);
    }

    @Override
    public Result insert(SysMenu menu) {
        log.info("该文件名是：SysMenuServiceImpl,该方法是：insert");
        MenuMapper.insert(menu);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("添加菜单数据成功");
    }

    @Override
    public Result delete(Long id) {
        log.info("该文件名是：SysMenuServiceImpl,该方法是：delete");
        MenuMapper.delete(id);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("删除菜单数据成功");
    }

    @Override
    public Result update(SysMenu menu) {
        log.info("该文件名是：SysMenuServiceImpl,该方法是：update");
        MenuMapper.update(menu);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("修改菜单数据成功");
    }

    @Override
    public Result findParent() {
        log.info("该文件名是：SysMenuServiceImpl,该方法是：findParent");
        return Result.success("查询所有父级菜单成功",MenuMapper.findParent());
    }
}
